<template>
  <div class="register-container">
    <div class="register-content">
      <div class="register-box">
        <div class="register-header">
          <GradientText text="创建账户" fontSize="2.5rem" />
          <p class="register-subtitle">加入 SDSE AIR，开启智能论文阅读之旅</p>
        </div>
        
        <div class="form-container">
          <div class="input-group">
            <label for="username">用户名</label>
            <input 
              id="username"
              v-model="registerForm.username" 
              type="text" 
              placeholder="请输入用户名"
              :class="{ 'error': errors.username }"
              @blur="checkUsername"
            />
            <span v-if="errors.username" class="error-message">{{ errors.username }}</span>
            <span v-if="usernameChecking" class="checking-message">检查中...</span>
          </div>
          
          <div class="input-group">
            <label for="password">密码</label>
            <input 
              id="password"
              v-model="registerForm.password" 
              type="password" 
              placeholder="请输入密码"
              :class="{ 'error': errors.password }"
            />
            <span v-if="errors.password" class="error-message">{{ errors.password }}</span>
          </div>
          
          <div class="input-group">
            <label for="confirmPassword">确认密码</label>
            <input 
              id="confirmPassword"
              v-model="registerForm.confirmPassword" 
              type="password" 
              placeholder="请再次输入密码"
              :class="{ 'error': errors.confirmPassword }"
            />
            <span v-if="errors.confirmPassword" class="error-message">{{ errors.confirmPassword }}</span>
          </div>
          
          <button 
            class="register-btn" 
            @click="handleRegister"
            :disabled="loading"
          >
            <span v-if="loading">注册中...</span>
            <span v-else>注<span style="margin-right: 12px;"></span>册</span>
          </button>
          
          <div class="login-link">
            <span>已有账户？</span>
            <a @click="goToLogin">立即登录</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GradientText from "@/components/GradientText.vue";

export default {
  name: 'RegisterPage',
  components: {
    GradientText,
  },
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      errors: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      loading: false,
      usernameChecking: false
    };
  },
  methods: {
    validateForm() {
      this.errors = {
        username: '',
        password: '',
        confirmPassword: ''
      };
      
      let isValid = true;
      
      if (!this.registerForm.username.trim()) {
        this.errors.username = '请输入用户名';
        isValid = false;
      } else if (this.registerForm.username.length < 3) {
        this.errors.username = '用户名至少需要3个字符';
        isValid = false;
      }
      
      if (!this.registerForm.password.trim()) {
        this.errors.password = '请输入密码';
        isValid = false;
      } else if (this.registerForm.password.length < 6) {
        this.errors.password = '密码至少需要6个字符';
        isValid = false;
      }
      
      if (!this.registerForm.confirmPassword.trim()) {
        this.errors.confirmPassword = '请确认密码';
        isValid = false;
      } else if (this.registerForm.password !== this.registerForm.confirmPassword) {
        this.errors.confirmPassword = '两次输入的密码不一致';
        isValid = false;
      }
      
      return isValid;
    },
    
    async checkUsername() {
      if (!this.registerForm.username.trim()) {
        return;
      }
      
      this.usernameChecking = true;
      
      try {
        const response = await fetch(`http://localhost:2626/api/user/check-username?username=${encodeURIComponent(this.registerForm.username)}`, {
          method: 'GET',
          credentials: 'include'
        });
        
        const result = await response.json();
        
        if (result.success && result.exists) {
          this.errors.username = '用户名已存在';
        } else if (result.success && !result.exists) {
          // 用户名可用，清除错误信息
          if (this.errors.username === '用户名已存在') {
            this.errors.username = '';
          }
        }
      } catch (error) {
        console.error('检查用户名错误:', error);
      } finally {
        this.usernameChecking = false;
      }
    },
    
    async handleRegister() {
      if (!this.validateForm()) {
        return;
      }
      
      this.loading = true;
      
      try {
        const formData = new FormData();
        formData.append('username', this.registerForm.username);
        formData.append('password', this.registerForm.password);
        
        const response = await fetch('http://localhost:2626/api/user/register', {
          method: 'POST',
          body: formData,
          credentials: 'include'
        });
        
        const result = await response.json();
        
        if (result.success) {
          this.$message.success('注册成功，请登录');
          this.$router.push('/login');
        } else {
          this.$message.error(result.message || '注册失败');
        }
      } catch (error) {
        console.error('注册错误:', error);
        this.$message.error('网络错误，请稍后重试');
      } finally {
        this.loading = false;
      }
    },
    
    goToLogin() {
      this.$router.push('/login');
    }
  }
};
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  background-color: rgb(247,249,253);
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-content {
  width: 100%;
  max-width: 480px;
  padding: 0 20px;
}

.register-box {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  opacity: 0;
  transform: translateY(30px);
  animation: fadeInUp 0.8s ease-out forwards;
}

@keyframes fadeInUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.register-subtitle {
  color: #666;
  font-size: 16px;
  margin-top: 10px;
}

.form-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-group label {
  font-weight: 600;
  color: #333;
  font-size: 14px;
}

.input-group input {
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 10px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.8);
}

.input-group input:focus {
  outline: none;
  border-color: #5842f2;
  box-shadow: 0 0 0 3px rgba(88, 66, 242, 0.1);
}

.input-group input.error {
  border-color: #ff4757;
}

.error-message {
  color: #ff4757;
  font-size: 12px;
  margin-top: 4px;
}

.checking-message {
  color: #666;
  font-size: 12px;
  margin-top: 4px;
  font-style: italic;
}

.register-btn {
  padding: 12px 50px;
  border-radius: 9999px;
  background-color: white;
  font-weight: 600;
  font-size: 18px;
  color: black;
  border: 1px solid black;
  cursor: pointer;
  transition: all 0.3s ease-out;
  margin-top: 10px;
}

.register-btn:hover:not(:disabled) {
  background-color: #e3e2e2;
  color: black;
  border: 1px solid black;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.register-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

.login-link a {
  color: #5842f2;
  text-decoration: none;
  font-weight: 600;
  cursor: pointer;
  margin-left: 5px;
}

.login-link a:hover {
  text-decoration: underline;
}

@media (max-width: 480px) {
  .register-box {
    padding: 30px 20px;
    margin: 0 10px;
  }
  
  .register-content {
    padding: 0 10px;
  }
}
</style> 